﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetDxComponents.ExtenderProviders.Language;
using DevExpress.XtraEditors;
using DevExpress.XtraTabbedMdi;
using DevExpress.XtraGrid;
using DevExpress.XtraBars;

namespace System
{
    public static class FormExt1
    {
        public static DialogResult ShowDxDialog(this Form form, string text, string caption,
            MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return XtraMessageBox.Show(form, text, caption, buttons, icon);
        }

        #region Info
        public static DialogResult ShowDxInfoDialog(this Form form, string text, string caption)
        {
            return ShowDxDialog(form, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static DialogResult ShowDxInfoDialog(this Form form, string text)
        {
            return ShowDxInfoDialog(form, text, Application.ProductName);
        }
        #endregion

        #region Error
        public static DialogResult ShowDxErrorDialog(this Form form, string text, string caption)
        {
            return ShowDxDialog(form, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static DialogResult ShowDxErrorDialog(this Form form, string text)
        {
            return ShowDxErrorDialog(form, text, Application.ProductName);
        }
        #endregion

        #region Warning
        public static DialogResult ShowDxWarningDialog(this Form form, string text, string caption)
        {
            return ShowDxDialog(form, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public static DialogResult ShowDxWarningDialog(this Form form, string text)
        {
            return ShowDxWarningDialog(form, text, Application.ProductName);
        }
        #endregion

        #region Yes No Question
        public static DialogResult ShowDxYNQuestionDialog(this Form form, string text, string caption)
        {
            return ShowDxDialog(form, text, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }

        public static DialogResult ShowDxYNQuestionDialog(this Form form, string text)
        {
            return ShowDxYNQuestionDialog(form, text, Application.ProductName);
        }
        #endregion

        #region Yes No Cancel Question
        public static DialogResult ShowDxYNCQuestionDialog(this Form form, string text, string caption)
        {
            return ShowDxDialog(form, text, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
        }

        public static DialogResult ShowDxYNCQuestionDialog(this Form form, string text)
        {
            return ShowDxYNCQuestionDialog(form, text, Application.ProductName);
        }
        #endregion

        #region Language

        public static DialogResult ShowDxLngDialog(this Form form, string text, string caption,
            MessageBoxButtons buttons, MessageBoxIcon icon, params object[] args)
        {
            return XtraMessageBox.Show(form, ProgNetLanguage.sGetString(text, args), caption, buttons, icon);
        }

        #region Info
        public static DialogResult ShowDxLngInfoDialog(this Form form, string text, string caption)
        {
            return ShowDxLngDialog(form, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static DialogResult ShowDxLngInfoDialog(this Form form, string text)
        {
            return ShowDxLngInfoDialog(form, text, Application.ProductName);
        }
        #endregion

        #region Error
        public static DialogResult ShowDxLngErrorDialog(this Form form, string text, string caption, params object[] args)
        {
            return ShowDxLngDialog(form, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Error, args);
        }

        public static DialogResult ShowDxLngErrorDialog(this Form form, string text)
        {
            return ShowDxLngErrorDialog(form, text, Application.ProductName);
        }
        public static DialogResult ShowDxLngErrorDialog(this Form form, string text, object[] args)
        {
            return ShowDxLngErrorDialog(form, text, Application.ProductName, args);
        }
        #endregion

        #region Warning
        public static DialogResult ShowDxLngWarningDialog(this Form form, string text, string caption, params object[] args)
        {
            return ShowDxLngDialog(form, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning, args);
        }

        public static DialogResult ShowDxLngWarningDialog(this Form form, string text)
        {
            return ShowDxLngWarningDialog(form, text, Application.ProductName);
        }
        public static DialogResult ShowDxLngWarningDialog(this Form form, string text, object[] args)
        {
            return ShowDxLngWarningDialog(form, text, Application.ProductName, args);
        }
        #endregion

        #region Yes No Question
        public static DialogResult ShowDxLngYNQuestionDialog(this Form form, string text, string caption, params object[] args)
        {
            return ShowDxLngDialog(form, text, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, args);
        }

        public static DialogResult ShowDxLngYNQuestionDialog(this Form form, string text)
        {
            return ShowDxLngYNQuestionDialog(form, text, Application.ProductName);
        }
        public static DialogResult ShowDxLngYNQuestionDialog(this Form form, string text, object[] args)
        {
            return ShowDxLngYNQuestionDialog(form, text, Application.ProductName, args);
        }
        #endregion

        #region Yes No Cancel Question
        public static DialogResult ShowDxLngYNCQuestionDialog(this Form form, string text, string caption, params object[] args)
        {
            return ShowDxLngDialog(form, text, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, args);
        }

        public static DialogResult ShowDxLngYNCQuestionDialog(this Form form, string text)
        {
            return ShowDxLngYNCQuestionDialog(form, text, Application.ProductName);
        }
        public static DialogResult ShowDxLngYNCQuestionDialog(this Form form, string text, object[] args)
        {
            return ShowDxLngYNCQuestionDialog(form, text, Application.ProductName, args);
        }
        #endregion
        #endregion


        public static void FinishEditBindings(this ContainerControl control)
        {
            if (control.ActiveControl != null)
            {
                if (control.ActiveControl is TextBoxMaskBox)
                {
                    if (control.ActiveControl.Parent != null &&
                        control.ActiveControl.Parent.Parent != null &&
                        control.ActiveControl.Parent.Parent is GridControl)
                    {
                        GridControl g = (control.ActiveControl.Parent.Parent as GridControl);
                        if (g.FocusedView != null)
                        {
                            g.FocusedView.CloseEditor();
                            g.FocusedView.UpdateCurrentRow();
                        }
                    }
                }
                if (control.ActiveControl is ContainerControl)
                {
                    (control.ActiveControl as ContainerControl).FinishEditBindings();
                }
                if (control.ActiveControl is GridControl)
                {
                    if ((control.ActiveControl as GridControl).FocusedView != null)
                    {
                        (control.ActiveControl as GridControl).FocusedView.CloseEditor();
                        (control.ActiveControl as GridControl).FocusedView.UpdateCurrentRow();
                    }
                }
                if (control.ActiveControl is PopupBaseEdit)
                {
                    (control.ActiveControl as PopupBaseEdit).ClosePopup();
                }
                if (control.ActiveControl.DataBindings.Count > 0)
                {
                    foreach (Binding b in control.ActiveControl.DataBindings)
                        b.WriteValue();
                }
            }
        }

        private static Dictionary<XtraTabbedMdiManager, int> mgrs = new Dictionary<XtraTabbedMdiManager, int>();
        private static Dictionary<Form, XtraTabbedMdiManager> forms = new Dictionary<Form, XtraTabbedMdiManager>();

        public static void SetMinTabSize(this DevExpress.XtraTabbedMdi.XtraTabbedMdiManager manager, int minWidth)
        {
            manager.PageAdded -= new DevExpress.XtraTabbedMdi.MdiTabPageEventHandler(manager_PageAdded);
            if (minWidth > 0)
                manager.PageAdded += new DevExpress.XtraTabbedMdi.MdiTabPageEventHandler(manager_PageAdded);
            if (!mgrs.ContainsKey(manager))
                mgrs.Add(manager, minWidth);
            else
                mgrs[manager] = minWidth;

            foreach (XtraMdiTabPage p in manager.Pages)
                manager.SetPageSize(p, minWidth);
        }

        static void manager_PageAdded(object sender, DevExpress.XtraTabbedMdi.MdiTabPageEventArgs e)
        {
            int minSize = -1;
            XtraTabbedMdiManager m = sender as XtraTabbedMdiManager;
            if (m != null)
            {
                minSize = mgrs[m];
                m.SetPageSize(e.Page, minSize);
            }
            e.Page.MdiChild.TextChanged += new EventHandler(MdiChild_TextChanged);
            e.Page.MdiChild.FormClosed += new FormClosedEventHandler(MdiChild_FormClosed);
            forms.Add(e.Page.MdiChild, m);
        }

        static void MdiChild_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                forms.Remove(sender as Form);
            }
            catch { }
        }

        static void MdiChild_TextChanged(object sender, EventArgs e)
        {
            Form f = sender as Form;
            try
            {
                XtraTabbedMdiManager m = forms[f];
                SetPageSize(m, m.Pages[f], mgrs[m]);
            }
            catch { }
        }

        public static void SetPageSize(this XtraTabbedMdiManager m, XtraMdiTabPage page, int minSize)
        {
            string text = page.MdiChild.Text.Trim();
            if (minSize > 0)
            {
                while (text.Mesure(m.Appearance.Font).Width < minSize)
                    text += " ";
            }
            page.Text = text;
        }

        #region Popup Menu do zamykania tabow
        public static void AddCloseTabMenu(this XtraTabbedMdiManager m, string CloseThisCaption = "Zamknij &Aktywną Kartę", string CloseOthersCaption = "Zamknij &Inne Karty", string CloseAllCaption = "Zamknij &Wszystkie Karty")
        {
            if (m != null && !menus.ContainsKey(m))
            {
                PopupMenu popupMenu2 = CreatePopupMenu(m, CloseThisCaption, CloseOthersCaption, CloseAllCaption);

                menus.Add(m, popupMenu2);
                m.MouseDown += new MouseEventHandler(m_MouseDown);
            }
        }
        
        private static PopupMenu CreatePopupMenu(XtraTabbedMdiManager m, string CloseThisCaption, string CloseOthersCaption, string CloseAllCaption)
        {
            DevExpress.XtraBars.PopupMenu popupMenu2;
            DevExpress.XtraBars.BarButtonItem bbiThis;
            DevExpress.XtraBars.BarButtonItem bbiOther;
            DevExpress.XtraBars.BarButtonItem bbiAll;
            DevExpress.XtraBars.BarManager barManager1;
            DevExpress.XtraBars.BarDockControl barDockControlTop;
            DevExpress.XtraBars.BarDockControl barDockControlBottom;
            DevExpress.XtraBars.BarDockControl barDockControlLeft;
            DevExpress.XtraBars.BarDockControl barDockControlRight;

            popupMenu2 = new DevExpress.XtraBars.PopupMenu();
            bbiThis = new DevExpress.XtraBars.BarButtonItem();
            bbiOther = new DevExpress.XtraBars.BarButtonItem();
            bbiAll = new DevExpress.XtraBars.BarButtonItem();
            barManager1 = new DevExpress.XtraBars.BarManager();
            barDockControlTop = new DevExpress.XtraBars.BarDockControl();
            barDockControlBottom = new DevExpress.XtraBars.BarDockControl();
            barDockControlLeft = new DevExpress.XtraBars.BarDockControl();
            barDockControlRight = new DevExpress.XtraBars.BarDockControl();
            ((System.ComponentModel.ISupportInitialize)(popupMenu2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(barManager1)).BeginInit();
            // 
            // popupMenu2
            // 
            popupMenu2.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
            new DevExpress.XtraBars.LinkPersistInfo(bbiThis),
            new DevExpress.XtraBars.LinkPersistInfo(bbiOther),
            new DevExpress.XtraBars.LinkPersistInfo(bbiAll)});
            popupMenu2.Manager = barManager1;
            popupMenu2.Name = "popupMenu2";
            // 
            // bbiThis
            // 
            bbiThis.Caption = CloseThisCaption;
            bbiThis.Id = 0;
            bbiThis.Name = "bbiThis";
            bbiThis.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbiZamknijTa_ItemClick);
            bbiThis.Tag = m;
            // 
            // bbiOther
            // 
            bbiOther.Caption = CloseOthersCaption;
            bbiOther.Id = 1;
            bbiOther.Name = "bbiOther";
            bbiOther.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbiZamknijInne_ItemClick);
            bbiOther.Tag = m;
            // 
            // bbiAll
            // 
            bbiAll.Caption = CloseAllCaption;
            bbiAll.Id = 2;
            bbiAll.Name = "bbiAll";
            bbiAll.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbiZamknijWszystkie_ItemClick);
            bbiAll.Tag = m;
            // 
            // barManager1
            // 
            barManager1.DockControls.Add(barDockControlTop);
            barManager1.DockControls.Add(barDockControlBottom);
            barManager1.DockControls.Add(barDockControlLeft);
            barManager1.DockControls.Add(barDockControlRight);
            barManager1.Form = m.MdiParent;
            barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
            bbiThis,
            bbiOther,
            bbiAll});
            barManager1.MaxItemId = 3;
            // 
            // barDockControlTop
            // 
            barDockControlTop.CausesValidation = false;
            barDockControlTop.Dock = System.Windows.Forms.DockStyle.Top;
            barDockControlTop.Location = new System.Drawing.Point(0, 0);
            barDockControlTop.Size = new System.Drawing.Size(292, 0);
            // 
            // barDockControlBottom
            // 
            barDockControlBottom.CausesValidation = false;
            barDockControlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            barDockControlBottom.Location = new System.Drawing.Point(0, 273);
            barDockControlBottom.Size = new System.Drawing.Size(292, 0);
            // 
            // barDockControlLeft
            // 
            barDockControlLeft.CausesValidation = false;
            barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
            barDockControlLeft.Location = new System.Drawing.Point(0, 0);
            barDockControlLeft.Size = new System.Drawing.Size(0, 273);
            // 
            // barDockControlRight
            // 
            barDockControlRight.CausesValidation = false;
            barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
            barDockControlRight.Location = new System.Drawing.Point(292, 0);
            barDockControlRight.Size = new System.Drawing.Size(0, 273);
            // 
            // Form1
            // 
            ((System.ComponentModel.ISupportInitialize)(popupMenu2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(barManager1)).EndInit();
            return popupMenu2;
        }
        
        static Dictionary<XtraTabbedMdiManager, PopupMenu> menus = new Dictionary<XtraTabbedMdiManager, PopupMenu>();
        
        static void m_MouseDown(object sender, MouseEventArgs e)
        {
            XtraTabbedMdiManager m = sender as XtraTabbedMdiManager;
            if (e.Button == MouseButtons.Right)
            {
                if (m != null && menus.ContainsKey(m))
                {
                    menus[m].ShowPopup(Control.MousePosition);
                }
            }
        }

        private static void bbiZamknijTa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraTabbedMdiManager xtraTabbedMdiManager1 = e.Item.Tag as XtraTabbedMdiManager;
            if (xtraTabbedMdiManager1!=null )
                if(xtraTabbedMdiManager1.SelectedPage != null)
                xtraTabbedMdiManager1.SelectedPage.MdiChild.Close();
        }

        private static void bbiZamknijInne_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
             XtraTabbedMdiManager xtraTabbedMdiManager1 = e.Item.Tag as XtraTabbedMdiManager;
             if (xtraTabbedMdiManager1 != null)
             {
                 List<DevExpress.XtraTabbedMdi.XtraMdiTabPage > l = new List<DevExpress.XtraTabbedMdi.XtraMdiTabPage>();
                 foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage page in xtraTabbedMdiManager1.Pages)
                     if (xtraTabbedMdiManager1.SelectedPage != page)
                         l.Add(page);
                 foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage p in l)
                     p.MdiChild.Close();
             }
        }

        private static void bbiZamknijWszystkie_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraTabbedMdiManager xtraTabbedMdiManager1 = e.Item.Tag as XtraTabbedMdiManager;
            if (xtraTabbedMdiManager1 != null)
            {
                List<DevExpress.XtraTabbedMdi.XtraMdiTabPage> l = new List<DevExpress.XtraTabbedMdi.XtraMdiTabPage>();
                foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage page in xtraTabbedMdiManager1.Pages)
                    l.Add(page);
                foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage p in l)
                    p.MdiChild.Close();
            }
        }
        #endregion
    }
}
